#!/bin/sh
. /usr/local/scripts/build_scripts/util.sh
cur_branch=$1
cur_channel=$2
cur_repo=$3
echo this is android $3 and channel=$cur_channel
git checkout $cur_branch
git pull
chmod 777 build.sh gradlew

if [ "$cur_repo" == "send_sdk" ];then
    if [ "$cur_branch" == "r1.0.1_shuwei_1.0" ];then
        echo "this is shuwei branch and  have no change_channel"
        git diff
        ./gradlew clean
        ./gradlew assembleRelease
        mkdir target
        cp -rn app/build/outputs/apk/* target/
        cp -rn sdk/build/outputs/aar/* target/
    else
        #r2.6.7 版本以及后面的版本脚本已经满足了sh build.sh channel，老版本不满足
        if [ "$cur_branch" == "r2.6.6_konka_1.0" ];then
            send_sdk_channel $cur_channel
            git diff
            sh build.sh |tee build_result
        else
            change_channel $cur_channel sdk
            git diff
            sh build.sh $cur_channel | tee build_result
        fi
        get_build_result
    fi
fi

if [ "$cur_repo" == "rec_sdk" ];then
    [ "$cur_channel" == "mi" ] && recsdk_mi
    if [  $cur_branch == "r5.4.0_kumiao_bugfix" -o $cur_branch == "r5.4.0_feature_huashu" -o $cur_branch == "r5.4.1_tcl_1.0" -o $cur_branch == "r5.4.0_douyu_1.0" ];then
        sh build.sh |tee build_result
        ls target/  |grep hpplay-sdk | tee modify_files
        rec_sdk_rename modify_files $cur_channel
    else
        change_channel $cur_channel sdk
        echo start build $cur_channel
        git diff
        sh build.sh $cur_channel | tee build_result
    fi
    zip -r target/libs.zip sdk/libs
    get_build_result
fi

if [ "$cur_repo" == "send_app" -o "$cur_repo" == "rec_app" ]; then 
    a_channel=`echo $cur_channel |sed 's/^../\u&/'`
    debug_task=assemble$a_channel"Debug"
    release_task=assemble$a_channel"Release"
    [ "$cur_repo" == "send_app" ] && channels_file="mobile_assistant"
    [ "$cur_repo" == "rec_app" ] && channels_file="all_channels"
    mkdir target
    cp /usr/local/scripts/build_scripts/$channels_file  ./

    if [ "$cur_repo" == "rec_app" -a "$cur_branch" == "r7.1.5_feixun_1.0" ];then
        git clone ssh://gerrit@192.168.8.250:29418/android/standard/AndroidReceiver-SDK
        cd AndroidReceiver-SDK
        git checkout leboapk_develop
        git pull
        chmod 777 gradlew build.sh
        sh build.sh feixun
        aar_name=`ls target |grep .aar$ |grep -v debug`
        cp target/$aar_name ../app/libs/sdk-release.aar
        cd ../
    fi

    if [ "$cur_channel" == "all_stores" ];then
        for b_channel in `cat $channels_file `
        do
            git reset --hard
            git pull
            echo build $b_channel
            [ "$app_repo" == "send_app" ] && send_app_channel  $b_channel
            git diff app/build.gradle
            ./gradlew assemble"$b_channel"Release |tee build_result
            get_build_result
         done
         cp app/build/outputs/apk/* target/
    elif [ "$cur_channel" == "all_apps" ];then
        ./gradlew assembleRelease |tee build_result
        get_build_result
        cp app/build/outputs/apk/* target/
    else
        ./gradlew clean
        git status
        echo "Start build apk......"
        [ "$cur_repo" == "send_app" ] && send_app_channel  $cur_channel
        [ "$cur_repo" == "rec_app" -a "$cur_channel" == "GIMI" ] && rec_app_gimi
        git diff
        echo "build $release_task"
        ./gradlew $release_task |tee build_result
        get_build_result
        cp app/build/outputs/apk/* target/
        echo "build $debug_task"
        ./gradlew clean
        ./gradlew $debug_task |tee build_result
        get_build_result
        cp app/build/outputs/apk/* target/
    fi
fi 
